package com.bst.bsj.bops.web.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.MemberService;
import com.bst.bsj.bops.web.biz.MessageService;
import com.bst.bsj.bops.web.biz.SpareNameConfigService;
import com.bst.bsj.bops.web.biz.UserMessageService;
import com.bst.bsj.common.dal.dataobject.Message;
import com.bst.bsj.common.dal.dataobject.SpareNameConfig;
import com.bst.bsj.common.dal.dataobject.UserMessage;
import com.bst.bsj.common.maopu.enums.HasReadEnum;
import com.bst.bsj.common.maopu.enums.MsgTypeEnum;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.StringUtil;

/**
* @Title: SystemConfigController.java
* @Package com.bst.bsj.bops.web.controller
* @Description: 商品有效期设置
* @author yj
* @date 2016年5月17日 上午10:42:32
* @version V1.0
* Copyright (c) 2015, 博思堂博视界.
 */
@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/spareNameConfig")
public class SpareNameConfigController extends WebBaseController {

  @Autowired
  private SpareNameConfigService spareNameConfigService;
 
  @Autowired
  private UserMessageService userMessageService;
  
  @Autowired
  private MessageService messageService;

  
  
  @RequestMapping(value = "/spare")
  public ModelAndView privilege(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    ModelAndView modelAndView = getBopsModelAndView("management/spare");
    Map<String, Object> params = getBopsParams(req);//参数
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    String baseName = (String) params.get("baseName");//名称
    String mobile = (String) params.get("mobile");
    String status = (String) params.get("status");
    
    if (StringUtil.isNotBlank(baseName)) {
      paramsMap.put("baseName", baseName);
    }
    if (StringUtil.isNotBlank(mobile)) {
      paramsMap.put("mobile", mobile);
    }
    if (StringUtil.isNotBlank(status)) {
      paramsMap.put("status", status);
    }
    
    String pageNums = (String) params.get("pageNum");
    Integer pageNum = 0;
    
    if (StringUtil.isNumeric(pageNums)) {
      pageNum = Integer.valueOf(pageNums);
    }
    if (pageNum == null || pageNum == 0) {
      pageNum = 1;
    }
    paramsMap.put("page", pageNum);
    paramsMap.put("length", new Long(10));
    paramsMap.put("offset", new Long((pageNum - 1) * 10));
    
    Result result = spareNameConfigService.query(paramsMap);
    // 如果请求成功
    if (result.isSuccess()) {
      // 得到分页器
      Paginator paginator = (Paginator) result.getModels().get("paginator");
      // 得到数据
      List<SpareNameConfig> spareNameConfigList = (List<SpareNameConfig>) result.getModels().get("spareNameConfigList");
      modelAndView.addObject("spareNameConfigList", spareNameConfigList);
      modelAndView.addObject("paginator", paginator);
    }
    modelAndView.addObject("params", params);
    
    return modelAndView;
  }
  
  
  @RequestMapping(value = "/changeStatus")
  @ResponseBody
  public void changeStatus(HttpServletRequest req, HttpServletResponse resp,String id) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    String status = (String) params.get("status");//状态
    String reason = (String) params.get("reason");//拒绝原因
    String baseName = (String) params.get("baseName");//苗木名称
    String uid = (String) params.get("uid");
    SpareNameConfig spare = new SpareNameConfig();
    spare.setId(Long.valueOf(id));
    spare.setStatus(status);
    spare.setReason(reason);
    int rId=spareNameConfigService.update(spare);
    
    if(rId>0){
    
    String okStr="尊敬的用户，您提交的添加苗木名称"+baseName+"已通过审核，现在您可以发布该苗木了。";
    String noStr="尊敬的用户，很抱歉地通知您，您提交添加苗木名称"+baseName+"未能通过审核，拒绝原因："+reason+"，如有疑问请致电客服，给您带来的不便敬请谅解。";
    
    String content=noStr;
    if("OK".equals(status)){
      content=okStr;
    }
    
    Message message=new Message();
    message.setTitle(content);
    message.setContent(content);
    message.setMsgType(MsgTypeEnum.SYS.getCode());
    Long mid=messageService.insert(message);
    
    Date date=new Date();
    Calendar   calendar   =   new   GregorianCalendar(); 
    calendar.setTime(date); 
    calendar.add(calendar.DATE,30);
    date=calendar.getTime();   //过期时间为30天
    
    List<UserMessage> umList=new ArrayList<UserMessage>();
    String formUid=(String)req.getSession().getAttribute("lastLoginid");
      UserMessage um=new UserMessage();
      um.setFromUid(formUid);
      um.setHasRead(HasReadEnum.NO.getCode());
      um.setMsgType(MsgTypeEnum.SYS.getCode());
      um.setUid(uid);
      um.setMsgId(mid);
      um.setExpireTime(date);
      umList.add(um);
     userMessageService.batchInsert(umList);
  }
    
  }

}
